package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.MinePoint2;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.MinePointDetailView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/7/6 0006.
 */

public class MinePointDetailPersenter extends BasePresent<MinePointDetailView> {

    RetrofitSerives retrofitSerives;
    Call<MinePoint2> pointCall;

    public void getMinePoint(int page, int pageSize, int userManagerId, int integralType) {

        view.showProgress();
        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("page", page);
            jsonObject.put("pageSize", pageSize);

            JSONObject param = new JSONObject();
            param.put("userManagerId", userManagerId);
            param.put("integralType", integralType);
            jsonObject.put("param", param);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        pointCall = retrofitSerives.getMinePoint(body);

        pointCall.enqueue(new Callback<MinePoint2>() {
            @Override
            public void onResponse(Call<MinePoint2> call, Response<MinePoint2> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {

                        view.setMinePoint(response.body());
                    } else {

                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<MinePoint2> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
